package com.tencent.bugly.bugly_crash

import android.os.Build
import com.tencent.bugly.crashreport.BuglyLog
import com.tencent.bugly.crashreport.CrashReport
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler

/**
 * Description:bugly oa futter plugin
 * @author rockypzhang
 * @since 2019/5/28
 */
class BuglyCrashPlugin : FlutterPlugin, ActivityAware, MethodCallHandler {
    private var activityAware: ActivityPluginBinding? = null
    private var channel: MethodChannel? = null


    /**
     * flutter层调用方法的native层实现
     *
     * @param call flutter方法回调
     * @param result 返回结果
     */
    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android " + Build.VERSION.RELEASE)
        } else if (call.method == "initCrashReport") {
            var appId: String? = ""
            var isDebug = false
            if (call.hasArgument("appId")) {
                appId = call.argument("appId")
            }
            if (call.hasArgument("isDebug")) {
                isDebug = call.argument("isDebug")!!
                BuglyCrashPluginLog.isEnable = isDebug
            }
            CrashReport.initCrashReport(activityAware?.activity, appId, isDebug)
            BuglyCrashPluginLog.d("onMethodCall initCrashReport")
        } else if (call.method == "postException") {
            var type: String? = ""
            var error: String? = ""
            var stackTrace: String? = ""
            var extraInfo: Map<String?, String?>? = null
            if (call.hasArgument("type")) {
                type = call.argument("type")
            }
            if (call.hasArgument("error")) {
                error = call.argument("error")
            }
            if (call.hasArgument("stackTrace")) {
                stackTrace = call.argument("stackTrace")
            }
            if (call.hasArgument("extraInfo")) {
                extraInfo = call.argument("extraInfo")
            }
            BuglyCrashPluginLog.d(
                "type:" + type + "error:" + error + " stackTrace:" + stackTrace
                        + "extraInfo:" + extraInfo
            )
            val category = 8
            CrashReport.postException(category, type, error, stackTrace, extraInfo)
        } else if (call.method == "setAppChannel") {
            var appChannel: String? = ""
            if (call.hasArgument("appChannel")) {
                appChannel = call.argument("appChannel")
            }
            CrashReport.setAppChannel(activityAware?.activity, appChannel)
        } else if (call.method == "setAppPackage") {
            var appPackage: String? = ""
            if (call.hasArgument("appPackage")) {
                appPackage = call.argument("appPackage")
            }
            CrashReport.setAppPackage(activityAware?.activity, appPackage)
        } else if (call.method == "setAppVersion") {
            var appVersion: String? = ""
            if (call.hasArgument("appVersion")) {
                appVersion = call.argument("appVersion")
            }
            CrashReport.setAppVersion(activityAware?.activity, appVersion)
            BuglyCrashPluginLog.d("mContext:" + activityAware?.activity + " appVersion:" + appVersion)
        } else if (call.method == "setUserSceneTag") {
            var userSceneTag = 0
            if (call.hasArgument("userSceneTag")) {
                userSceneTag = call.argument("userSceneTag")!!
            }
            CrashReport.setUserSceneTag(activityAware?.activity, userSceneTag)
            BuglyCrashPluginLog.d("mContext:" + activityAware?.activity + " appVersion:" + userSceneTag)
        } else if (call.method == "setUserId") {
            val userSceneTag = 0
            var userId: String? = ""
            if (call.hasArgument("userId")) {
                userId = call.argument("userId")
            }
            CrashReport.setUserId(activityAware?.activity, userId)
            BuglyCrashPluginLog.d("mContext:" + activityAware?.activity + " appVersion:" + userSceneTag)
        } else if (call.method == "putUserData") {
            var userKey: String? = ""
            var userValue: String? = ""
            if (call.hasArgument("userKey")) {
                userKey = call.argument("userKey")
            }
            if (call.hasArgument("userValue")) {
                userValue = call.argument("userValue")
            }
            CrashReport.putUserData(activityAware?.activity, userKey, userValue)
            BuglyCrashPluginLog.d("userKey:$userKey userValue:$userValue")
        } else if (call.method.contains("log")) {
            buglyLog(call)
        } else if (call.method.contains("setIsDevelopmentDevice")) {
            var isDevelopmentDevice = false
            if (call.hasArgument("isDevelopmentDevice")) {
                isDevelopmentDevice = call.argument("isDevelopmentDevice")!!
            }
            CrashReport.setIsDevelopmentDevice(activityAware?.activity, isDevelopmentDevice)
            BuglyCrashPluginLog.d("isDevelopmentDevice:$isDevelopmentDevice")
        } else {
            result.notImplemented()
        }
    }

    /**
     * 打印上报用户自定义日志
     *
     * @param call flutter方法回调
     * @param content 内容
     */
    private fun buglyLog(call: MethodCall) {
        var tag: String? = ""
        var content: String? = ""
        if (call.hasArgument("tag")) {
            tag = call.argument("tag")
        }
        if (call.hasArgument("content")) {
            content = call.argument("content")
        }
        if (call.method == "logd") {
            BuglyLog.d(tag, content)
        } else if (call.method == "logi") {
            BuglyLog.i(tag, content)
        } else if (call.method == "logv") {
            BuglyLog.v(tag, content)
        } else if (call.method == "logw") {
            BuglyLog.w(tag, content)
        } else if (call.method == "loge") {
            BuglyLog.e(tag, content)
        }
        BuglyCrashPluginLog.d("tag:$tag content:$content")
    }

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "bugly")
        channel?.setMethodCallHandler(this)
    }

    override fun onDetachedFromEngine(plugin: FlutterPlugin.FlutterPluginBinding) {
        channel?.setMethodCallHandler(null)
    }


    override fun onAttachedToActivity(plugin: ActivityPluginBinding) {
        this.activityAware = plugin
    }

    override fun onDetachedFromActivityForConfigChanges() {
        onDetachedFromActivity()
    }

    override fun onDetachedFromActivity() {
        activityAware = null
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        onAttachedToActivity(binding)
    }
}